package cn.stylefeng.guns.modular.util;


import com.fasterxml.jackson.annotation.JsonIgnore;


/**
 * 描述：API返回结果, 所有对外API接口必须通过此包装
 *
 * @author He Yong
 * @version 1.0
 * @since 2018/1/31
 */
public class Result {

    /**
     * 结果码, {@link ResultCode}
     */
    private Integer code;

    /**
     * 结果信息，一般接口错误时返回错误信息
     */
    private String msg;

    /**
     * 结果数据
     */
    private Object data;


    private Long total;

    private Long pageNum;


    public Long getTotalPage() {
        return total;
    }

    public void setTotalPage(Long totalPage) {
        this.total = totalPage;
    }

    public Long getPageNum() {
        return pageNum;
    }

    public void setPageNum(Long pageNum) {
        this.pageNum = pageNum;
    }

    public Result() {
    }

    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static Result success() {
        Result result = new Result();
        result.setResultCode(ResultCode.SUCCESS);
        result.setData("");
        return result;
    }

    public static Result success(Object data, Long totalPage, Long pageNum) {
        Result result = new Result();
        result.setResultCode(ResultCode.SUCCESS);
        result.setData(data);
        result.setTotalPage(totalPage);
        result.setPageNum(pageNum);
        return result;
    }

    public static Result success(Object data) {
        return success().data(data);
    }

    public static Result failure(ResultCode resultCode) {
        Result result = new Result();
        result.setResultCode(resultCode);
        return result;
    }

    public static Result failure(ResultCode resultCode, String msg) {
        return failure(resultCode).msg(msg);
    }

    public Result code(Integer code) {
        this.code = code;
        return this;
    }

    public Result msg(String msg) {
        this.msg = msg;
        return this;
    }

    public Result data(Object data) {
        this.data = data;
        return this;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    /**
     * 防止返回空前端处理问题
     */
    public Object getData() {
        return this.data;
    }

    public void setData(Object data) {
        this.data = data;
    }


    @JsonIgnore
    public boolean isSuccess() {
        return this.code == ResultCode.SUCCESS.code();
    }

    public void setResultCode(ResultCode code) {
        this.code = code.code();
        this.msg = code.msg();
    }
}
